/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GameHallClient;

import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;

import WebServiceModel.GamingStateModel;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;

import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
/**
 *
 * @author Peach
 */
public class GameHallClient {
    public GameHallClient(){
        try {
            url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = "gameHallService";
	methodQuit = "quitGame";
	methodEnter = "takeGame";
        methodGetFreeList="getFreeDeskList";
        methodExchange="exchangeTurn";
    }
    
    public GameHallClient(String URI,String quit,String enter,
            String getList,String exchange){
        try {
            url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = URI;
	methodQuit = quit;
	methodEnter = enter;
        methodGetFreeList=getList;
        methodExchange=exchange;
    }
    
    public void quitGame(int id, int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodQuit);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("id", int.class, id, null));
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
    }
    
    public void takeGame(int id, int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodEnter);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("id", int.class, id, null));
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public ArrayList<Integer> getFreeDeskList(int room){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodGetFreeList);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("room", int.class, room, null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                System.out.println(result.getValue());
                return (ArrayList<Integer>)result.getValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	return null;
    }
    
    public void exchangeTurn(int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodExchange);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /*
    public static void main(String[] args){
        GameHallClient test =new GameHallClient();
        test.quitGame(567, 1, 2);
        //test.quitGame(123, 1, 2);
    }
    */
    
    private URL url;
    private String uri;
    private String methodQuit;
    private String methodEnter;
    private String methodGetFreeList;
    private String methodExchange;
}
